// pages/user/information/index.js
import {
  updateInformation
} from '../../../api/user.js';
import {
  getInvoiceInfo
} from '../../../api/other.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isChange: false,
    avatarUrl: '',
    nickName: '',
    email: '',
    mobile: '',
    invoiceType: 0, // 0-个人 1-公司
    title: '',
    companyTitle: '',
    taxNum: '',
    roleId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      avatarUrl,
      nickName,
    } = getApp().globalData.userInfo;
    const roleId = getApp().globalData.roleId;
    getInvoiceInfo().then(res => {
      this.setData({
        avatarUrl,
        nickName,
        roleId,
        email: res.email,
        companyTitle: res.companyTitle,
        taxNum: res.taxNum,
        title: res.title,
        invoiceType: res.taxNum ? 1 : 0
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      mobile: getApp().globalData.userInfo.mobile,
    })
  },

  /**
   * 保存修改
   */
  handleChangeInformation() {
    if (this.data.nickName === '') {
      this.showToast('请输入昵称')
    } else {
      updateInformation({
        companyTitle: this.data.companyTitle,
        title: this.data.title,
        taxNum: this.data.taxNum,
        image: this.data.avatarUrl,
        nickName: this.data.nickName,
        email: this.data.email,
      }).then(() => {
        this.showToast('修改成功');
        getApp().globalData.userInfo.nickName = this.data.nickName;
        getApp().globalData.userInfo.email = this.data.email;
        this.setData({
          isChange: false
        })
      })
    }
  },

  /**
   * 发票类型切换
   * @param {*} e 
   */
  handleChangeInvoiceType(e) {
    this.setData({
      invoiceType: Number(e.currentTarget.dataset.type)
    })
  },

  /**
   * 跳转修改手机号
   */
  handleNavigatorChangeMobile() {
    wx.navigateTo({
      url: '/pages/user/mobile/change',
    })
  },

    /**
   * 跳转简历
   */
  handleNavigatorChangeResume() {
    wx.navigateTo({
      url: '/pages/resume/preview',
    })
  },

      /**
   * 跳转简历预览
   */
  handleNavigatorChangePreview(){
    wx.navigateTo({
      url: '/pages/resume/preview',
    })
  },

  /**
   * 昵称
   * @param {object} e 
   */
  onInputNickName(e) {
    this.data.nickName = e.detail.value.trim();
    this.setData({
      isChange: true
    })
  },

  /**
   * 邮箱
   * @param {object} e 
   */
  onInputEmail(e) {
    this.data.email = e.detail.value.trim();
    this.setData({
      isChange: true
    })
  },

  /**
   * 个人发票抬头
   * @param {object} e 
   */
  onInputUserTitle(e) {
    this.data.userTitle = e.detail.value.trim();
    this.setData({
      isChange: true
    })
  },

  /**
   * 企业发票抬头
   * @param {object} e 
   */
  onInputCompanyTitle(e) {
    this.data.companyTitle = e.detail.value.trim();
    this.setData({
      isChange: true
    })
  },

  /**
   * 企业税号
   * @param {object} e 
   */
  onInputTaxNumber(e) {
    this.data.taxNum = e.detail.value.trim();
    this.setData({
      isChange: true
    })
  },

  /**
   * 吐司
   * @param {string} msg 
   */
  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  }
})